//
//  NMPOSersListViewController.swift
//  NMPO
//
//  Created by PowerfullClean on 2023/10/31.
//

import UIKit

class NMPOSersListViewController: NMPOBaseCollectionViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
       
        nmpo_confiListData()
        NotificationCenter.default.addObserver(self, selector: #selector(nmpo_updateList), name: nmpo_vpnlistUpdate_noti, object: nil)
        
        
    }
    // MARK: - nmpo action
    override func nmpo_layout() -> UICollectionViewFlowLayout {
        let nmpo_layout = UICollectionViewFlowLayout()
        nmpo_layout.itemSize = CGSize(width: nmpo_SCREEN_WIDTH-28, height: 68)
        nmpo_layout.headerReferenceSize = CGSize(width: nmpo_SCREEN_WIDTH-28, height: 58)
        nmpo_layout.minimumLineSpacing = 5
        return nmpo_layout
    }
    override func nmpo_collectionViews() {
        super.nmpo_collectionViews()
        view.addSubview(nmpo_navView)
        nmpo_collectionView.snp.makeConstraints { make in
            make.left.width.bottom.equalToSuperview()
            make.top.equalTo(nmpo_navView.snp.bottom)
        }
       
        nmpo_collectionView.register(NMPOSersListHeadView.classForCoder(), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: NMPOSersListHeadView.nmpo_viewToString)
        nmpo_collectionView.dataSource = self
        nmpo_collectionView.delegate = self
        nmpo_collectionView.contentInset = UIEdgeInsets(top: 0, left: 14, bottom: NMPOCommon.nmpo_tabbarH+15, right: 14)
        nmpo_collectionView.register(NMPOSersListCell.classForCoder(), forCellWithReuseIdentifier: NMPOSersListCell.nmpo_viewToString)
    }
    func nmpo_confiListData() {
        NMPOManager.nmpo_manager.nmpo_updateDataBaseSmartAddressNoPing()
        let nmpo_smartModel = NMPOManager.nmpo_manager.nmpo_getSmartServerModel()
        if let nmpo_smartModel = nmpo_smartModel {
            nmpo_serverModels.append([nmpo_smartModel])
        }
        let nmpo_serModels = NMPOManager.nmpo_manager.nmpo_getAllServerModels()
        if nmpo_serModels.count == 0 {
            NMPOManager.nmpo_manager.nmpo_updateDataBaseListAddress()
            let nmpo_serModels2 = NMPOManager.nmpo_manager.nmpo_getAllServerModels()
            if nmpo_serModels2.count != 0 {
                nmpo_serverModels.append(nmpo_serModels2)
            }
        }else {
            nmpo_serverModels.append(nmpo_serModels)
        }
//        NMPOManager.nmpo_manager.nmpo_ipInfoVPNAction(nmpo_serModels: nmpo_serModels)
       
    }
    @objc func nmpo_updateList() {
        if nmpo_serverModels.count == 0 || nmpo_serverModels.count == 1 {
          nmpo_serverModels.removeAll()
            nmpo_confiListData()
          nmpo_collectionView.reloadData()
        }
       
    }
    // MARK: - nmpo lazy
    override func nmpo_baseBgName() -> String? {
        return "nmpo_list_bg"
    }
    lazy var nmpo_navView: NMPOServerNavView = {
        let nmpo_navView = NMPOServerNavView()
        nmpo_navView.nmpo_titleLabel.text = "Servers List"
        return nmpo_navView
    }()
    var nmpo_serverModels = [[NMPOSerModel]]()
//    var nmpo_selectIndexPath = IndexPath(item: 0, section: 0)
}
extension NMPOSersListViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return nmpo_serverModels.count
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return nmpo_serverModels[section].count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let nmpo_cell = collectionView.dequeueReusableCell(withReuseIdentifier: NMPOSersListCell.nmpo_viewToString, for: indexPath) as! NMPOSersListCell
        let nmpo_serverModel = nmpo_serverModels[indexPath.section][indexPath.row]
        if indexPath.section == 0 {
            nmpo_cell.nmpo_bgView.image = UIImage(named: "nmpo_list_smart")
//            nmpo_cell.nmpo_iconView.image = UIImage(named: "nmpo_home_changeIcon")
        }else {
            nmpo_cell.nmpo_bgView.image = UIImage(named: "nmpo_list_other")
        }
        
        nmpo_cell.nmpo_serverModel = nmpo_serverModel
        
        return nmpo_cell
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let nmpo_headView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: NMPOSersListHeadView.nmpo_viewToString, for: indexPath) as! NMPOSersListHeadView
        if indexPath.section == 0 {
            nmpo_headView.nmpo_titleLabel.text = "Smart location"
        }else {
            nmpo_headView.nmpo_titleLabel.text = "Other location"
        }
        return nmpo_headView
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        NMPOManager.nmpo_manager.nmpo_updateSelectServerModels()
//        nmpo_serverModels[nmpo_selectIndexPath.section][nmpo_selectIndexPath.row].nmpo_select = false
          let nmpo_serverModel = nmpo_serverModels[indexPath.section][indexPath.row]
//        if nmpo_serverModel.nmpo_smart {
//            NMPOManager.nmpo_manager.nmpo_connectSmartAction()
//        }else {
//        nmpo_serverModel.nmpo_select = true
//          NMPOManager.nmpo_manager.nmpo_connectListAction(nmpo_serverModel: nmpo_serverModel)
//        }
        nmpo_serverModel.nmpo_select = true
        NMPOManager.nmpo_manager.nmpo_updateDataModel()
        NMPOManager.nmpo_manager.nmpo_connectSelectAddreddAction()
        navigationController?.popToRootViewController(animated: true)
        NMPOManager.nmpo_manager.nmpo_smart = false
        NMPOManager.nmpo_manager.nmpo_best = false
        
    }
}
